<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="body" v-else>
		<!--  列表  -->
		<template v-if="purchaseList && purchaseList.length > 0">
			<view :left-options="options" v-for="item in purchaseList">
				<view class="panel bottomBorder" @click="openDetail(item)">
					<view class="panel_title" :class="{error: item.error}">
						<view class="panel_title_left">{{ item.materialName }}</view>
					</view>
					<view class="panel_content">
						<view class="panel_content_row">
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title">订单编号：</view>
								<view class="panel_content_row_item_value">{{ item.billNo }}</view>
							</view>
						</view>
						<view class="panel_content_row">
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title">总重量：</view>
								<view class="panel_content_row_item_value">{{ item.count }}</view>
							</view>
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title">总箱数：</view>
								<view class="panel_content_row_item_value">{{ item.boxCount }}</view>
							</view>
						</view>
						<view class="panel_content_row">
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title">创建人：</view>
								<view class="panel_content_row_item_value">{{ item.createUserName || item.taskUserName }}</view>
							</view>
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title" v-if="item.produceHouse">生产车间：</view>
								<view class="panel_content_row_item_value">{{ item.produceHouse }}</view>
							</view>
						</view>
						<view class="panel_content_row">
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title">创建时间：</view>
								<view class="panel_content_row_item_value">{{ formatTime(item.createTime) }}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</template>
		<uni-empty-info v-else label="暂无订单信息"></uni-empty-info>
	</view>
</template>
<script>
	import dayjs from 'dayjs'
	import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"

	export default {
		components: {
			uniLoading,
			uniEmptyInfo,
		},
		data() {
			return {
				loading: false,
				purchaseList: [],
				produceHouse: '',
				billNo: '',
				taskBillNo: '',
			}
		},
		async onLoad(e) {
			// 判断哪种类型订单的列表
			if (e.type) this.type = e.type
			if (e.produceHouse) this.produceHouse = e.produceHouse
			if (e.billNo) this.billNo = e.billNo
			if (e.taskBillNo) this.taskBillNo = e.taskBillNo
			this.initData()
		},
		methods: {
			/** 初始化数据 */
			initData() {
				this.pageConfig = {
					pageIndex: 0,
					pageSize: 20,
					bottomEnd: false,
					bottomLoading: true
				}
				this.getPurchaseList()
			},

			/**
			 * 查看详情
			 */
			async openDetail(material) {
				if (this.type === "outStork") {
					uni.setStorageSync("billNo", material.billNo);
					uni.navigateBack()
				} else if (this.type === 'get') {
					this.$navigateTo("/pages/production/taskPicking", {
						orderId: material.billNo,		// 订单编号
						taskBillNo: this.taskBillNo,	// 任务单编号
						billNo: this.billNo,			// 物料单编号
						produceHouse: this.produceHouse // 生产车间
					})	
				} else if (this.type === 'fill') {
					this.$navigateTo("/pages/production/taskFill", {
						orderId: material.billNo,		// 订单编号
						taskBillNo: this.taskBillNo,	// 任务单编号
						billNo: this.billNo,			// 物料单编号
						produceHouse: this.produceHouse // 生产车间
					})
				}
			},

			/** 获取数据 */
			async getPurchaseList() {
				let {
					res,
					data = []
				} = await this.$api.getMaterialPickBillTask({
					taskBillNo: this.taskBillNo,
					type: this.type === "get" ? "pick" : (this.type === "fill" ? "supplement" : "other")
				});
				if (!res) return;
				this.purchaseList = data;
				this.loading = false;
				this.pageConfig.bottomEnd = true;
				this.pageConfig.bottomLoading = false;
				uni.stopPullDownRefresh();
			},

			/**
			 * 
			 * @param {Object} e
			 */
			formatTime(e) {
				return dayjs(e).format("YYYY-MM-DD HH:mm:ss")
			},

			/**
			 * 下拉刷新数据
			 */
			onPullDownRefresh() {
				this.initData()
			},

			/** 上滑加载 */
			// onReachBottom() {
			// 	if (!this.pageConfig.bottomEnd) {
			// 		this.pageConfig.pageIndex++
			// 		this.pageConfig.bottomLoading = true
			// 		this.getPurchaseList();
			// 	}
			// },
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../static/css/common";

	.error {
		color: red;
	}

	.save {
		width: 140px !important;
	}
</style>
